package com.dg11185.nearshop.app;

import java.math.BigDecimal;

import com.dg11185.libs.utils.string.StringUtils;

/**
 * 单位转换，如将米转换为千米，分秒转化为小时等
 * 
 * @author：Daibo
 * @time：2014-5-6 上午10:07:00
 */
public class ConvertUnit {

    /**
     * 地图相关功能中，将自带的米转为直接用于显示的String
     */
    public static String meter2KiloMeter(String m) {
        return meter2KiloMeter(Double.parseDouble(m));
    }

    public static String meter2KiloMeter(int m) {
        return meter2KiloMeter(m + 0.0);
    }

    public static String meter2KiloMeter(double m) {
        int decimalPoint = 0; // 记录小数点位置，保留小数点后一位
        if (m > 1000) {
            String s = m / 1000 + "";
            if (s.contains(".")) {
                decimalPoint = s.indexOf(".");
                return s.substring(0, decimalPoint + 2) + " 公里";
            }
            return s + " 公里";
        } else {
            String s = m + "";
            if (s.contains(".")) {
                decimalPoint = s.indexOf(".");
                return s.substring(0, decimalPoint + 2) + " 米";
            }
            return m + " 米";
        }
    }

    public static String second2Minute(String s) {
        int m = Integer.parseInt(s);
        return second2Minute(m);
    }

    public static String second2Minute(int s) {
        return s / 60 + " 分钟";
    }

    public static String getDistance(String latitude1, String longitude1, String latitude2, String longitude2) {
        return getDistance(Double.parseDouble(latitude1), Double.parseDouble(longitude1),
                Double.parseDouble(latitude2), Double.parseDouble(longitude2));

    }

    public static String getDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
        double lat1 = (Math.PI / 180) * latitude1;
        double lat2 = (Math.PI / 180) * latitude2;

        double lon1 = (Math.PI / 180) * longitude1;
        double lon2 = (Math.PI / 180) * longitude2;

        // 地球半径
        double R = 6378.1;

        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1))
                * R;

        double distance = new BigDecimal(d).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        return meter2KiloMeter(distance * 1000);
    }

    public static double getDistance1(double latitude1, double longitude1, double latitude2, double longitude2) {
        double lat1 = (Math.PI / 180) * latitude1;
        double lat2 = (Math.PI / 180) * latitude2;

        double lon1 = (Math.PI / 180) * longitude1;
        double lon2 = (Math.PI / 180) * longitude2;

        // 地球半径
        double R = 6378.1;

        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1))
                * R;
        try {

            double distance = new BigDecimal(d).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            return distance * 1000;
        } catch (Exception e) {
            return 0;
        }
    }

    // 20140523053436 to 2014-05-23 05:34:36
    public static String dateConvert(String date) {
        String s = "";
        if (date == null || "".equals(date)) {
            return "";
        }
        if (date.length() < 14 || !StringUtils.isNumeric(date)) {
            s = date;
        } else {
            StringBuilder builder = new StringBuilder(date.substring(0, 4));
            builder.append("-").append(date.subSequence(4, 6)).append("-").append(date.subSequence(6, 8)).append(" ")
                    .append(date.subSequence(8, 10)).append(":").append(date.subSequence(10, 12)).append(":")
                    .append(date.subSequence(12, 14));
            s = builder.toString();
        }
        return s;
    }

    final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    public static String[] convertGeopoint(String latitude, String longitude) {
        String[] point = new String[2];
        double x = Double.parseDouble(latitude);
        double y = Double.parseDouble(longitude);

        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(x * x_pi);
        double theta = Math.atan2(x, y) + 0.000003 * Math.cos(y * x_pi);

        point[0] = z * Math.sin(theta) + 0.006 + "";
        point[1] = z * Math.cos(theta) + 0.0065 + "";
        return point;
    }

    /** 这里的username用于显示在界面，所以部分信息不会公开, 电话号码或者邮箱会部分用星号代替 */
    public static String getShortUserName() {
        String userName = "";
        if (MyApplication.getUser() != null) {
            userName = MyApplication.getUser().userName;

            if (userName == null || "".equals(userName)) {
                if (StringUtils.isMobilePhone(MyApplication.getUser().mobilePhone)) {
                    userName = MyApplication.getUser().mobilePhone.substring(0, 3) + "****"
                            + MyApplication.getUser().mobilePhone.substring(7);
                }
            }

            if (userName == null || "".equals(userName)) {
                String email = MyApplication.getUser().email;
                if (StringUtils.isEmail(email)) {
                    int index = email.indexOf("@");
                    if (index <= 4) {
                        userName = "****" + email.substring(index);
                    } else {
                        userName = email.substring(0, 4) + "****" + userName + email.substring(index);
                    }
                }
            }
        }
        return userName;
    }

    /**
     * 获取完整的userName 用于提交数据
     */
    public static String getFullUserName() {
        String userName = "";
        if (MyApplication.getUser() != null) {
            userName = MyApplication.getUser().userName;

            if (userName == null || "".equals(userName)) {
                userName = MyApplication.getUser().mobilePhone;
            }

            if (userName == null || "".equals(userName)) {
                userName = MyApplication.getUser().email;
            }
        }
        return userName;
    }
}
